'''
Created on May 10, 2012

@author: thomas_dao
'''

from web import base
from web.base import BaseHandler
from web import urls
from entity.entity import Group
from web import util
import json
import logging
from google.appengine.ext import ndb
from google.appengine.ext.ndb import model
from webapp2_extras.appengine.auth.models import User

def add_participants(group_id=None, participants=None):
    # Build group key from id
    group_key = model.Key(Group, group_id)
    
    # Parse participants id into array

            
        
class CreateHandler(BaseHandler):
    @base.user_required
    def post(self):
        group = Group()
        name = self.request.params.get('name')
        if name is not None:
            group.name = name
        
        # If participants in params, add them to the group
        participants = self.request.params.get('participants')
        logging.info(participants)
        if participants is not None:
            participant_ids = participants.split(',')
            user_list = []
            user_list.append(util.get_current_user_key())
            for user_id in participant_ids:
                user_list.append(model.Key(User, user_id))
                
            group.participants = user_list
        
        # Save to datastore
        group.put()
            
        self.response.write(json.dumps(util.ndb_to_dict(group)))
    

class ReadHandler(BaseHandler):
    def get(self):
        pass
    

class UpdateHandler(BaseHandler):
    def post(self):
        pass
    

class DeleteHandler(BaseHandler):
    def post(self):
        pass

    
app = urls.app

def main():
    app.run()

if __name__ == '__main__':
    main()